<?php

namespace app\admin\controller;

use app\model\UserPassport as ModelUserPassport;
use app\model\UserOrder as ModelUserOrder;
use app\model\UserInfo as ModelUserInfo;

class Statistical extends Member
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 交易数据 \n
     * URI : /admin/trading.data
     * @param :
     *  name       | type   | null | description
     * ------------|--------|------|-------------
     *  passport   | string | 必填  | 凭证
     *  startGmt   | string | 选填  | （例：startGmt = '2018-05-01 00:00:00';//）（开始时间必须前与结束时间，并且间隔不超过60天）
     *  endGmt     | string | 选填  | （例：endGmt = '2018-05-30 23:59:59';//）
     *
     * @return :
     * name        | type   | description
     * ------------|--------|-------------
     *  list       | array  | 文字数组
     *  numImg     | array  | 曲线图数组
     *
     * list:
     *  name                | type  | description
     * ---------------------|-------|-------------
     *  viewsNum            | int   | 浏览量
     *  orderUserNum        | int   | 下单人数(下单的用户)
     *  orderNumber         | int   | 订单数 (已付款的人数)
     *  effectiveOrderNum   | int   | 有效订单数 （已完成的订单数量）
     *  totalOrderAmount    | float | 下单总金额（只要有订单的都算）
     *  payingPeople        | int   | 付款人数
     *  paymentOrder        | int   | 付款订单数
     *  paymentMoney        | float | 付款金额
     *  orderConversionRate | int   | 下单转化率(单位%)
     *  paymentConversionRate | int | 付款转化率(单位%)
     *  dealConversionRate  | int   | 成交转化率(单位%)
     *
     * numImg:
     *  name                | type   | description
     * ---------------------|--------|-------------
     * paymentMoney         | float  | 付款金额
     * payingPeople         | int    | 付款人数
     * paymentOrder         | int    | 付款订单数
     * orderConversionRate  | float  | 下单转化率
     * paymentConversionRate| float  | 付款转化率
     * dealConversionRate   | float  | 成交转化率
     * gmt                  | float  | 时间
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function tradingData(){
        $arr_data = [];
        $str_start_gmt = $this->_getContentParams('startGmt'); // 开始时间
        $str_end_gmt =  $this->_getContentParams('endGmt'); // 结束时间
        $this->publicStartEnd($str_start_gmt, $str_end_gmt);

        $arr_where = $this->tradingDataWhere($str_start_gmt, $str_end_gmt);

        $arr_num_img = $this->salesNumImg($str_start_gmt, $str_end_gmt);
        $arr_data['numImg'] = $arr_num_img;

        //浏览量
        $arr_data['viewsNum'] = (int)ModelUserPassport::getInstance()->toCount(['gmt_create' => $arr_where]);
        //下单人数
        $arr_data['orderUserNum'] = (int)ModelUserOrder::getInstance()->toGroup(['is_delete' => 0, 'gmt_create' => $arr_where], 'user_id');
        //订单数
        $arr_data['orderNumber'] = (int)ModelUserOrder::getInstance()->toCount(['is_delete' => 0, 'gmt_create' => $arr_where]);
        //有效订单数
        $arr_data['effectiveOrderNum'] = (int)ModelUserOrder::getInstance()->toCount(['order_state' => ['EGT', 10], 'is_unusual' => 1, 'is_delete' => 0, 'gmt_create' => $arr_where]);
        //下单总金额
        $arr_data['totalOrderAmount'] = (float)ModelUserOrder::getInstance()->toSum(['order_state' => ['EGT', 10], 'is_unusual' => 1, 'is_delete' => 0, 'gmt_create' => $arr_where], 'order_amount');
        //付款人数
        $arr_data['payingPeople'] = (int)ModelUserOrder::getInstance()->toGroup(['order_state' => ['EGT', 30], 'is_unusual' => 1, 'is_delete' => 0, 'gmt_create' => $arr_where], 'user_id');
        //付款订单数
        $arr_data['paymentOrder'] = (int)ModelUserOrder::getInstance()->toCount(['order_state' => ['EGT', 30], 'is_unusual' => 1, 'is_delete' => 0, 'gmt_create' => $arr_where]);
        //付款总金额
        $arr_data['paymentMoney'] = (float)ModelUserOrder::getInstance()->toSum(['order_state' => ['EGT', 30], 'is_unusual' => 1, 'is_delete' => 0, 'gmt_create' => $arr_where], 'order_amount');

        $arr_data = $this->tradingDataJudge($arr_data);
        _returnData(['list' => $arr_data]);
    }

    /**
     * @param string $str_start_gmt
     * @param string $str_end_gmt
     * @return array
     */
    public function salesNumImg($str_start_gmt = '', $str_end_gmt = ''){
        $arr_list = [];
        $arr_list_info = [];
        $str_day = $this->whereDay($str_start_gmt, $str_end_gmt);
        $str_start_gmt = strtotime($str_start_gmt);

        for($i = 0; $i < $str_day; $i++){
            $arr_where = ['between', [date('Y-m-d 00:00:00',$str_start_gmt) ,date('Y-m-d 23:59:59',$str_start_gmt)]];
            //每天的付款总金额
            $arr_list['paymentMoney'][$i] = (float)ModelUserOrder::getInstance()->toSum(['order_state' => ['EGT', 30], 'is_unusual' => 1, 'is_delete' => 0, 'gmt_create' => $arr_where], 'order_amount');
            //每天的付款人数
            $arr_list['payingPeople'][$i] = (int)ModelUserOrder::getInstance()->toGroup(['order_state' => ['EGT', 30], 'is_unusual' => 1, 'is_delete' => 0, 'gmt_create' => $arr_where], 'user_id');
            //每天的付款订单数
            $arr_list['paymentOrder'][$i] = (int)ModelUserOrder::getInstance()->toCount(['order_state' => ['EGT', 30], 'is_unusual' => 1, 'is_delete' => 0, 'gmt_create' => $arr_where]);
            //每天的浏览量
            $arr_list_info['viewsNum'][$i] = (int)ModelUserPassport::getInstance()->toCount(['gmt_create' => $arr_where]);
            //每天的下单人数
            $arr_list_info['orderUserNum'][$i] = (int)ModelUserOrder::getInstance()->toGroup(['is_delete' => 0, 'gmt_create' => $arr_where], 'user_id');

            if($arr_list_info['orderUserNum'][$i] == 0){
                $arr_list['orderConversionRate'][$i] = 0;
            }else{
                //下单转换率
                $arr_list['orderConversionRate'][$i] = round(($arr_list_info['orderUserNum'][$i]/$arr_list_info['viewsNum'][$i])*100, 2);
            }
            if($arr_list['payingPeople'][$i] == 0){
                $arr_list['paymentConversionRate'][$i] = 0;
                $arr_list['turnoverRate'][$i] = 0;
            }
            else{
                //付款转化率
                $arr_list['paymentConversionRate'][$i] = round(($arr_list['payingPeople'][$i]/$arr_list_info['orderUserNum'][$i])*100, 2);
                //成交转化率
                $arr_list['turnoverRate'][$i] = round(($arr_list['payingPeople'][$i]/$arr_list_info['viewsNum'][$i])*100, 2);
            }
            $arr_list['gmt'][$i] = date('Y-m-d', $str_start_gmt);
            $str_start_gmt = ($str_start_gmt + 86400);
        }
        return $arr_list;
    }

    /**
     * @param $str_end_gmt
     * @param $str_start_gmt
     * @param $int_days
     * @param $int_error
     * @return float|int
     */
    public function whereDay($str_start_gmt, $str_end_gmt, $int_days = 61, $int_error = 30101){
        $str_day = (strtotime($str_end_gmt) - strtotime($str_start_gmt))/86400;
        _toCompare($str_day, '>', $int_days, $int_error);
        return ceil($str_day);
    }

    /**
     * @param $arr_data
     * @return mixed
     */
    public function tradingDataJudge($arr_data){

        if($arr_data['orderUserNum'] == 0){
            $arr_data['orderConversionRate'] = 0;
        }else{
            //下单转换率
            $arr_data['orderConversionRate'] = round(($arr_data['orderUserNum']/$arr_data['viewsNum'])*100, 2);
        }

        if($arr_data['payingPeople'] == 0){
            $arr_data['paymentConversionRate'] = 0;
            $arr_data['turnoverRate'] = 0;
        }else{
            //付款转化率
            $arr_data['paymentConversionRate'] = round(($arr_data['payingPeople']/$arr_data['orderUserNum'])*100, 2);
            //成交转化率
            $arr_data['turnoverRate'] = round(($arr_data['payingPeople']/$arr_data['viewsNum'])*100, 2);
        }
        return $arr_data;
    }

    /**
     * @param string $str_start_gmt
     * @param string $str_end_gmt
     * @return array
     */
    public function tradingDataWhere($str_start_gmt = '', $str_end_gmt = ''){
        $arr_where = ['between', [$str_start_gmt, $str_end_gmt]];
        return $arr_where;
    }

    /**
     * 公用开始时和结束时间的判断
     * @param $str_start_gmt
     * @param $str_end_gmt
     * @return float 天数
     */
    public function publicStartEnd($str_start_gmt, $str_end_gmt){
        _toCompare(empty($str_start_gmt), '===', true, 30001);
        _toCompare(empty($str_end_gmt), '===', true, 30002);
        _toCompare($str_start_gmt, '>', $str_end_gmt, 30003);
        $str_day = (strtotime($str_end_gmt) - strtotime($str_start_gmt))/86400;
        return ceil($str_day);
    }

    /**
     * 新老客户交易构成 \n
     * URI : /admin/new.and.old.trading
     * @param :
     *  name       | type   | null | description
     * ------------|--------|------|-------------
     *  passport   | string | 必填  | 凭证
     *  date       | string | 必填  | 格式 （例：date = "2018-04"）
     *
     * @return :
     * name        | type   | description
     * ------------|--------|-------------
     *  list       | array  | 文字数组
     *
     * list:
     *  name                | type   | description
     * ---------------------|--------|-------------
     *  thinNewUserNum      | int    | 本月新用户付款人数
     *  thinOldUserNum      | int    | 本月老用户付款人数
     *  thinNewUserNumThan  | string | 新用户付款人数较前一月
     *  thinOldUserNumThan  | string | 老客户付款人数较前一月
     *
     *  thinNewUserMoney    | float   | 本月新用户付款金额
     *  thinOldUserMoney    | float   | 本月老用户付款金额
     *  thinNewUserMoneyThan| string  | 本月新用户付款金额较前一月
     *  thinOldUserMoneyThan| string  | 本月老用户付款金额较前一月
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function newAndOldTrading(){

        $str_date = $this->_getContentParams('date'); // 格式 2018-04
        $str_time = $str_date . '-01 00:00:00';
        $arr_data = [];

        // 本月新用户查询条件
        $arr_now_new_where = $this->getWhere($str_time, 'now', 'new');

        // 本月老用户查询条件
        $arr_now_old_where = $this->getWhere($str_time, 'now', 'old');

        // 上月新用户查询条件
        $arr_last_new_where = $this->getWhere($str_time, 'last', 'new');

        // 上月老用户查询条件
        $arr_last_old_where = $this->getWhere($str_time, 'last', 'old');

        // 本月新用户付款人数
        $arr_data['thinNewUserNum'] = (int)ModelUserOrder::getInstance()->getUserNum($arr_now_new_where);
        // 本月老用户付款人数
        $arr_data['thinOldUserNum'] = (int)ModelUserOrder::getInstance()->getUserNum($arr_now_old_where);
        // 上月新用户付款人数
        $last_new_user_num =   (int) ModelUserOrder::getInstance()->getUserNum($arr_last_new_where);
        // 上月老用户付款人数
        $last_old_user_num = (int)ModelUserOrder::getInstance()->getUserNum($arr_last_old_where);

        // 本月新用户付款金额
        $arr_data['thinNewUserMoney'] = (float)ModelUserOrder::getInstance()->getUserTotalMoney($arr_now_new_where);
        // 本月老用户付款金额
        $arr_data['thinOldUserMoney'] = (float)ModelUserOrder::getInstance()->getUserTotalMoney($arr_now_old_where);
        // 上月新用户付款金额
        $last_new_user_money = (float)ModelUserOrder::getInstance()->getUserTotalMoney($arr_last_new_where);
        // 上月老用户付款金额
        $last_old_user_money = (float)ModelUserOrder::getInstance()->getUserTotalMoney($arr_last_old_where);

        $arr_data = $this->newOldThan($arr_data, $last_new_user_num, $last_old_user_num, $last_new_user_money, $last_old_user_money);

        _returnData($arr_data);
    }

    /**
     * @param $arr_data
     * @param $last_new_user_num
     * @param $last_old_user_num
     * @param $last_new_user_money
     * @param $last_old_user_money
     * @return mixed
     */
    public function newOldThan($arr_data, $last_new_user_num, $last_old_user_num, $last_new_user_money, $last_old_user_money){
        if($arr_data['thinNewUserNum'] == 0 || $last_new_user_num == 0){
            $arr_data['thinNewUserNumThan'] = '/';
        }else{
            $arr_data['thinNewUserNumThan'] = round((($arr_data['thinNewUserNum'] - $last_new_user_num)/$arr_data['thinNewUserNum'])*100, 2);
        }
        if($arr_data['thinOldUserNum'] == 0 || $last_old_user_num == 0){
            $arr_data['thinOldUserNumThan'] = '/';
        }else{
            $arr_data['thinOldUserNumThan'] = round((($arr_data['thinOldUserNum'] - $last_old_user_num)/$arr_data['thinOldUserNum'])*100, 2);
        }
        if($arr_data['thinNewUserMoney'] == 0 || $last_new_user_money == 0){
            $arr_data['thinNewUserMoneyThan'] = '/';
        }else{
            $arr_data['thinNewUserMoneyThan'] = round((($arr_data['thinNewUserMoney'] - $last_new_user_money)/$arr_data['thinNewUserMoney'])*100, 2);
        }
        if($arr_data['thinOldUserMoney'] == 0 || $last_old_user_money == 0){
            $arr_data['thinOldUserMoneyThan'] = '/';
        }else{
            $arr_data['thinOldUserMoneyThan'] = round((($arr_data['thinOldUserMoney'] - $last_old_user_money)/$arr_data['thinOldUserMoney'])*100, 2);
        }
        return $arr_data;
    }

    /**
     * 获取查询条件
     * @param $str_time
     * @param string $str_type
     * @param string $str_type2
     * @return array
     */
    public function getWhere($str_time, $str_type = 'last', $str_type2 = 'old')
    {
        // 老用户ID
        $arr_user_ids = ModelUserOrder::getInstance()->paymentTimeUser($str_time);
        $arr_user_ids = array_unique($arr_user_ids);

        if ($str_type2 == 'new') {
// 新用户ID
            $arr_user_ids = ModelUserInfo::getInstance()->noPaymentTimeUser($arr_user_ids);
        }

        if ($str_type == 'now') {
            $str_next_time = date("Y-m-d 00:00:00", strtotime("$str_time +1 month"));
            // 本月新客户查询条件
            $arr_where = ['gmt_modified' => ['between', [$str_time, $str_next_time]], 'user_id' => ['IN', $arr_user_ids]];
        } else {
            $str_last_time = date("Y-m-d 00:00:00", strtotime("$str_time -1 month"));
// 上月新客户查询条件
            $arr_where = ['gmt_modified' => ['between', [$str_last_time, $str_time]], 'user_id' => ['IN', $arr_user_ids]];
        }
        return $arr_where;
    }

    /**
     * 交易数据(金额) \n
     * URI : /admin/money.trading.data
     * @param :
     *  name       | type   | null | description
     * ------------|--------|------|-------------
     *  passport   | string | 必填  | 凭证
     *  startGmt   | string | 选填  | （例：startGmt = '2018-03-15 06:35:35'）（开始时间必须前与结束时间，并且间隔不超过60天）
     *  endGmt     | string | 选填  | （例：endGmt = '2018-03-15 06:35:35'）
     *
     * @return :
     * name        | type   | description
     * ------------|--------|-------------
     *  list       | array  | 数组
     *
     * list:
     *  name     | type   | description
     * ----------|--------|-------------
     *  0        | int    | 0-50 人数
     *  1        | int    | 51-100 人数
     *  2        | int    | 101-200 人数
     *  3        | int    | 201-500 人数
     *  4        | int    | 501-1000 人数
     *  5        | int    | 1001-5000 人数
     *  6        | int    | 5001-10000 人数
     *  7        | int    | 10001 以上人数
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function moneyTradingData(){
        $str_start_gmt = $this->_getContentParams('startGmt'); // 开始时间
        $str_end_gmt = $this->_getContentParams('endGmt'); // 结束时间
        if($str_start_gmt != '' && $str_end_gmt != ''){
            $str_day = (strtotime($str_end_gmt) - strtotime($str_start_gmt))/86400;
            _toCompare($str_day, '>', 61, 30101);
            _toCompare($str_start_gmt < $str_end_gmt, '===', false, 19110);
        }
        $arr_where = $this->tradingDataWhere($str_start_gmt, $str_end_gmt);
        $arr_data = $this->paidAmount($arr_where);
        _returnData($arr_data);
    }

    /**
     * @param $arr_where
     * @return array
     */
    public function paidAmount($arr_where){
        $arr_data = [];

        $arr_where_scope = [
            ['between', [0 ,50]],
            ['between', [51 ,100]],
            ['between', [101 ,200]],
            ['between', [201 ,500]],
            ['between', [501 ,1000]],
            ['between', [1001 ,5000]],
            ['between', [5001 ,10000]],
            ['EGT', 10001],
        ];
        foreach ($arr_where_scope as $value) {
            $arr_data[] = ModelUserOrder::getInstance()->toCount(['is_delete' => 0, 'order_state' => ['EGT', 30], 'gmt_create' => $arr_where, 'order_amount' => $value]);
        }
        return $arr_data;
    }

    /**
     * 会员增长情况 \n
     * URI : /admin/member.statistics
     * @param :
     *  name       | type   | null | description
     * ------------|--------|------|-------------
     *  passport   | string | 必填  | 凭证
     *  gmtStart   | string | 必填  | 格式 （例：date = "2018-01-01 00:00:00"）
     *  gmtEnd     | string | 必填  | 格式 （例：date = "2018-12-31 23:59:59"）
     *
     * @return :
     * name        | type   | description
     * ------------|--------|-------------
     *  list       | array  | 折线数组
     *
     * list:
     *  name       | type   | description
     * ------------|--------|-------------
     *  num        | int    | 数量
     *  time       | string | 日期
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function memberStatistics(){
        $str_start_gmt = $this->_getContentParams('gmtStart'); // 开始时间
        $str_end_gmt = $this->_getContentParams('gmtEnd'); // 结束时间
        $str_day = $this->publicStartEnd($str_start_gmt, $str_end_gmt);
        $arr_data = $this->memberStatisticsList($str_start_gmt, $str_day);
        _returnData($arr_data);
    }

    /**
     * @param $str_start_gmt
     * @param $str_day
     * @return array
     */
    public function memberStatisticsList($str_start_gmt, $str_day){
        $arr_data = [];
        $arr_num = [];
        $arr_time = [];
        $str_sunday = strtotime($str_start_gmt);
        for($i = 0; $i < $str_day; $i++)
        {
            $arr_num[$i] = ModelUserInfo::getInstance()->toCount(['gmt_create' => ['between', [date('Y-m-d 00:00:00', $str_sunday),date('Y-m-d 23:59:59', $str_sunday)]]]);
            $arr_time[$i] = date('m-d', $str_sunday);
            $str_sunday = ($str_sunday + 86400);
        }
        $arr_data['num'] = $arr_num;
        $arr_data['time'] = $arr_time;
        return $arr_data;
    }

    /**
     * 会员消费金额分布 \n
     * URI : /admin/member.consumption.amount
     * @param :
     *  name       | type   | null | description
     * ------------|--------|------|-------------
     *  passport   | string | 必填  | 凭证
     *  startGmt   | string | 选填  | （例：startGmt = '2018-03-15 06:35:35'）（开始时间必须前与结束时间，并且间隔不超过60天）
     *  endGmt     | string | 选填  | （例：endGmt = '2018-03-15 06:35:35'）
     *
     * @return :
     * name        | type   | description
     * ------------|--------|-------------
     *  list       | array  | 数组
     *
     * list:
     *  name     | type   | description
     * ----------|--------|-------------
     *  0        | int    | 0-50 人数
     *  1        | int    | 51-100 人数
     *  2        | int    | 101-200 人数
     *  3        | int    | 201-500 人数
     *  4        | int    | 501-1000 人数
     *  5        | int    | 1001-5000 人数
     *  6        | int    | 5001-10000 人数
     *  7        | int    | 10001 以上人数
     */
    public function memberConsumptionAmount(){
        $str_start_gmt = $this->_getContentParams('startGmt'); // 开始时间
        $str_end_gmt = $this->_getContentParams('endGmt'); // 结束时间
        if($str_start_gmt != '' && $str_end_gmt != ''){
            $str_day = (strtotime($str_end_gmt) - strtotime($str_start_gmt))/86400;
            _toCompare($str_day, '>', 61, 30101);
            _toCompare($str_start_gmt < $str_end_gmt, '===', false, 19110);
        }
        $arr_where = $this->tradingDataWhere($str_start_gmt, $str_end_gmt);
        $arr_data = $this->paidAmount($arr_where);
        _returnData($arr_data);
    }

    /**
     * @param $arr_where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function memberConsumptionAmountData($arr_where){
        $arr_data = [];
        $arr_where_scope = [
            ['between', [0 ,50]],
            ['between', [51 ,100]],
            ['between', [101 ,200]],
            ['between', [201 ,500]],
            ['between', [501 ,1000]],
            ['between', [1001 ,5000]],
            ['between', [5001 ,10000]],
            ['EGT', 10001],
        ];
        foreach ($arr_where_scope as $value) {
            $arr_data[] = ModelUserOrder::getInstance()->toGroupSum(['is_delete' => 0, 'order_state' => ['EGT', 30], 'gmt_create' => $arr_where, 'order_amount' => $value], 'user_id' ,'sum(order_amount)');
        }
        return $arr_data;
    }

    /**
     * 综合统计 \n
     * URI : /admin/comprehensive.statistics
     * @param :
     *  name       | type   | null | description
     * ------------|--------|------|-------------
     *  passport   | string | 必填  | 凭证
     *  startGmt   | string | 选填  | （例：startGmt = '2018-03-15 06:35:35'）（开始时间必须前与结束时间，并且间隔不超过90天）
     *  endGmt     | string | 选填  | （例：endGmt = '2018-03-15 06:35:35'）
     *
     * @return :
     * name        | type   | description
     * ------------|--------|-------------
     *  list       | array  | 数组
     *
     * list:
     *  name     | type   | description
     * ----------|--------|-------------
     *  logisticsRevenueSum     | float | 物流总收入
     *  effectiveOrderNum       | int   | 有效订单数
     *  totalOrderAmount        | float | 有效订单总金额
     *  completeOrderNum        | int   | 完成订单数
     *  completeOrderAmount     | float | 完成订单总金额
     *  userNum                 | int   | 会员总数
     *  userOrderNum            | int   | 有订单会员数
     *  userOrderCount          | int   | 会员订单总数
     *  totalMailAmount         | float | 会员寄件总额
     *  userSendRate            | float | 会员寄件率
     *  userAgeOrder            | float | 平均每会员订单数
     *  userAgeOrderSum         | float | 平均每会员寄件总金额
     */
    public function comprehensiveStatistics(){
        $str_start_gmt = $this->_getContentParams('startGmt'); // 开始时间
        $str_end_gmt = $this->_getContentParams('endGmt'); // 结束时间
        $arr_where = ['between', ['2018-01-01 00:00:00', '2058-12-31 23:59:59']];
        if ($str_start_gmt != '' && $str_end_gmt != ''){
            $str_day = $this->whereDay($str_start_gmt, $str_end_gmt, 91, 30102);
            $arr_where = $this->tradingDataWhere($str_start_gmt, $str_end_gmt);
        }
        $arr_data = [];

        //物流总收入
        $arr_data['logisticsRevenueSum'] = (float)ModelUserOrder::getInstance()->toSum(['gmt_create' => $arr_where, 'is_delete' => 0, 'order_state' => ['EGT', 30]], 'order_amount');
        //有效订单数
        $arr_data['effectiveOrderNum'] = (int)ModelUserOrder::getInstance()->toCount(['order_state' => ['EGT', 10], 'is_unusual' => 1, 'is_delete' => 0, 'gmt_create' => $arr_where]);
        //有效订单总金额
        $arr_data['totalOrderAmount'] = (float)ModelUserOrder::getInstance()->toSum(['order_state' => ['EGT', 10], 'is_unusual' => 1, 'is_delete' => 0, 'gmt_create' => $arr_where], 'order_amount');
        //完成订单数
        $arr_data['completeOrderNum'] = (int)ModelUserOrder::getInstance()->toCount(['order_state' => ['EGT', 80], 'is_unusual' => 1, 'is_delete' => 0, 'gmt_create' => $arr_where]);
        //完成订单总金额
        $arr_data['completeOrderAmount'] = (float)ModelUserOrder::getInstance()->toSum(['order_state' => ['EGT', 80], 'is_unusual' => 1, 'is_delete' => 0, 'gmt_create' => $arr_where], 'order_amount');
        //会员总数
        $arr_data['userNum'] = (int)ModelUserInfo::getInstance()->toCount([]);
        //有订单会员数
        $arr_data['userOrderNum'] = (int)ModelUserOrder::getInstance()->toGroup(['order_state' => ['EGT', 10], 'is_unusual' => 1, 'is_delete' => 0, 'gmt_create' => $arr_where], 'user_id');
        //会员订单总数
        $arr_data['userOrderCount'] = (int)ModelUserOrder::getInstance()->toCount([]);
        //会员寄件总额
        $arr_data['totalMailAmount'] = (float)ModelUserOrder::getInstance()->toSum(['order_state' => ['EGT', 30], 'is_unusual' => 1, 'is_delete' => 0, 'gmt_create' => $arr_where], 'order_amount');
        if($arr_data['userOrderNum'] == 0){
            $arr_data['userSendRate'] = 0;
        }else{
            //会员寄件率
            $arr_data['userSendRate'] = round(($arr_data['userOrderNum']/$arr_data['userNum'])*100, 2);
        }
        if($arr_data['userNum'] == 0){
            $arr_data['userAgeOrder'] = 0;
        }else{
            //平均每会员订单数
            $arr_data['userAgeOrder'] = round(($arr_data['userNum']/$arr_data['userNum'])*100, 2);
        }
        if($arr_data['totalOrderAmount'] == 0){
            $arr_data['userAgeOrderSum'] = 0;
        }else{
            //平均每会员寄件总金额
            $arr_data['userAgeOrderSum'] = round(($arr_data['totalOrderAmount']/$arr_data['userAgeOrder'])*100, 2);
        }
        _returnData($arr_data);
    }

    /**
     * 会员排行列表 \n
     * URI : /admin/members.ranking.list
     * @param :
     *  name       | type   | null | description
     * ------------|--------|------|-------------
     *  passport   | string | 必填  | 凭证
     *  gmtStart   | string | 必填  | 格式 （例：date = "2018-04-01 00:00:00"）
     *  gmtEnd     | string | 必填  | 格式 （例：date = "2018-04-02 23:59:59"）
     *
     *  page       | int    | 必填  | 页码
     *  listRows   | int    | 必填  | 一页的条数
     *
     * @return :
     * name             | type   | description
     * -----------------|--------|-------------
     *  totalPages      | string | 总页数
     *  userName        | string | 用户昵称
     *  orderNum        | int    | 订单数
     *  orderMoney      | int    | 订单金额
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function membersRankingList(){
        $int_now_page = $this->_getContentParams('page'); // 当前页码
        _toValidate('id', $int_now_page, 10005);
        $int_list_rows = $this->_getContentParams('listRows'); // 一页的条数
        _toValidate('id', $int_list_rows, 10006);
        $str_start_gmt = $this->_getContentParams('startGmt'); // 开始时间
        $str_end_gmt = $this->_getContentParams('endGmt'); // 结束时间
        $arr_gmt_where = ['between', ['2018-01-01 00:00:00', '2058-12-31 23:59:59']];
        if ($str_start_gmt != '' && $str_end_gmt != ''){
            $str_day = $this->whereDay($str_start_gmt, $str_end_gmt, 91, 30102);
            $arr_gmt_where = $this->tradingDataWhere($str_start_gmt, $str_end_gmt);
        }
        $arr_where = [
            'order_state' => ['EGT', 30],
            'is_unusual' => 1,
            'is_delete' => 0,
            'gmt_create' => $arr_gmt_where
        ];
        $int_total_rows = ModelUserOrder::getInstance()->toGroup($arr_where, 'user_id');
        $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
        $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询
        $str_field = 'user_id AS userId,COUNT(order_id) AS orderNum,SUM(order_amount) AS orderMoney';
        $arr_data = ModelUserOrder::getInstance()->toSelect($arr_where, $str_field, $int_first_row, $int_list_rows, 'orderMoney desc', 'user_id');
        $arr_data = $this->membersRankingListModify($arr_data);
        _returnData(['totalPages' => $int_total_pages, 'list' => $arr_data]);
    }

    /**
     * 商品销售情况列表 - 查出用户昵称
     * @param $arr_data
     * @return mixed
     */
    private function membersRankingListModify($arr_data){
        foreach ($arr_data as &$value) {
            $value['userName'] = (string)ModelUserInfo::getInstance()->toValue(['user_id' => $value['userId']], 'nickname');
        }
        return $arr_data;
    }

}
